﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.Page
{
    public partial class hhTuvan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int month = DateTime.Now.Month;
                int year = DateTime.Now.Year;
                DateTime lastday = GetLastDayOfMonth(year, month);
                DateTime dtfrom = GetfirstDayOfMonth(year, month);
                dteFrom.Date = dtfrom;
                dteTo.Date = lastday;
                cbxLoai.SelectedIndex = 0;
                Bindata();
            }
        }

        public static DateTime GetLastDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, DateTime.DaysInMonth(year, month));
        }

        public static DateTime GetfirstDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, 1);
        }

        protected void ASPxCallbackPanel1_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            Bindata();
        }

        void Bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                UserDB user_obj = (UserDB)Session[Global.SESS_USER];
                dx.Connection.Open();
                if (!cbxLoai.Value.ToString().Equals("0"))
                {
                    var list = from k in dx.log_HoaHong_tuvan_tructieps
                               where k.UerID == user_obj.ID && k.Ngaygiaodich >= dteFrom.Date && k.Ngaygiaodich <= dteTo.Date && k.type == int.Parse(cbxLoai.Value.ToString())
                               select new { k.FullName, k.Hoahong, k.Loai, k.Chuyentien, k.Ngaygiaodich, k.Ngaychuyen, k.Dadong };
                    grvResult.DataSource = list;
                    grvResult.DataBind();
                }
                else
                {
                    var list = from k in dx.log_HoaHong_tuvan_tructieps
                               where k.UerID == user_obj.ID && k.Ngaygiaodich >= dteFrom.Date && k.Ngaygiaodich <= dteTo.Date
                               select new { k.FullName, k.Hoahong, k.Loai, k.Chuyentien, k.Ngaygiaodich, k.Ngaychuyen, k.Dadong };
                    grvResult.DataSource = list;
                    grvResult.DataBind();
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        protected void grvResult_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            try
            {
                double hasError = double.Parse((e.GetValue("Dadong")).ToString());
                if (hasError < 590 && hasError > 99)
                {
                    e.Row.ForeColor = System.Drawing.Color.FromName("#007ACC");
                }
                else if (hasError < 100)
                    e.Row.ForeColor = System.Drawing.Color.Red;
            }
            catch (Exception) { }
        }

        protected void btnTim_Click(object sender, EventArgs e)
        {
            Bindata();
        }

        protected void grvResult_Load(object sender, EventArgs e)
        {
            Bindata();
        }
    }
}